/**
 * Copyright (c) 2012-2013 JLLT Corp. 2012-2013,All Rights Reserved.
 * This software is published under the 金麟龙腾商贸有限公司.
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file. 
 *
 * @File name:  UserService.java 
 * @Create on:  2012-06-25 21:04:407
 * @Author   :  陈伟
 *
 * @ChangeList
 * ---------------------------------------------------
 * Date         Editor              ChangeReasons
 *
 */

package com.boho.ecp.manage.user.service;

import java.sql.Connection;
import java.util.Date;

import cn.boho.framework.context.ActionContext;
import cn.boho.framework.exception.UserOperationException;
import cn.boho.framework.po.DynaBeanMap;
import cn.boho.framework.po.POUtils;
import cn.boho.framework.service.MessageService;

import com.boho.ecp.constant.User;
import com.boho.ecp.manage.user.dao.TSystemUserDao;
import com.boho.ecp.po.TSystemUserPO;
import com.boho.ecp.util.MD5Digester;

public class UserService {

	
	public static void login(ActionContext atx, String name, String password) throws Exception{
		Connection con = atx.getConection();
		password = MD5Digester.digest(password);
		DynaBeanMap dbm = TSystemUserDao.getTSystemUserByLogin(con, name, password);
		if(dbm != null){
			
			Integer userId = (Integer)dbm.get("SYSTEM_USER_ID");
			TSystemUserPO conditionTSystemUserPO = new TSystemUserPO();
			TSystemUserPO valueTSystemUserPO = new TSystemUserPO();
			conditionTSystemUserPO.setSystemUserId(userId);
			valueTSystemUserPO.setLastLoginTime(new Date());
			valueTSystemUserPO.setLastLoginIp(atx.getRemoteAddr());
			POUtils.update(con, conditionTSystemUserPO, valueTSystemUserPO);
			
			atx.setSessionValue(User.USER_SESSEION, dbm);
			atx.setSessionValue(User.USER_NO_SESSION, userId);
		}
		else{
			UserOperationException uoe = new UserOperationException();
			atx.setErrorContext("USER_LOGIN_FAIL", MessageService.getMessage("USER_LOGIN_FAIL"), uoe);
			throw uoe;
		}
	}
	
}
